module V{
    /** 鱼 */
    export class BuyuFish{
        /** 动画对象 */
        public mv:egret.MovieClip;
        /** 位置列表 */
        private pos:Array<number>;
        /** 位置数量 */
        private posCount:number;
        /** 当前位置 */
        private posCur:number;
        /** 鱼id */
        public fishid:number;
        /** 鱼配置 */
        private static fishRes = [[1,14,"fish0"],[15,17,"fish1"],[18,19,"fish2"],[20,27,"fish3"]];

        /** 构造函数 */
        public constructor(fn:number, p:Array<number>, pc:number, fid:number){
            this.updata(fn, p, pc, fid);
        }

        /** 更新资源 */
        public updata(fn:number, p:Array<number>, pc:number, fid:number):void{
            //console.log("addfish", fn);
            let fnr = "";
            for(let i=0;i<BuyuFish.fishRes.length;i++){
                if(BuyuFish.fishRes[i][0] <= fn && fn <= BuyuFish.fishRes[i][1]){
                    fnr = BuyuFish.fishRes[i][2].toString();
                    break;
                }
            }

            this.mv = D.App.getMovieClip(fnr, fn.toString());
            if(this.mv == null){
                this.posCount = 0;
                this.posCur = 2;
                console.log("fisherror", fn);
                return;
            }
            this.mv.anchorOffsetX = this.mv.width / 2;
            this.mv.anchorOffsetY = this.mv.height / 2;
            this.mv.gotoAndPlay("Move", -1);
            //console.log("frameLabels", this.mv.frameLabels);
            this.pos = p;
            this.posCount = pc * 2;
            this.posCur = 0;
            this.fishid = fid;

            this.mv.x = this.pos[0];
            this.mv.y = this.pos[1];
            this.finish();
        }

        /** 动画播放完成 */
        private finish():void{
            this.posCur += 2;
            if(this.posCur >= this.posCount){
                this.mv && this.mv.parent && this.mv.parent.removeChild(this.mv);
                //console.log("fishend", this.fishid);
                return;
            } 

            let time = Math.sqrt(Math.pow(this.pos[this.posCur]-this.mv.x,2)+Math.pow(this.pos[this.posCur+1]-this.mv.y,2))/6*100;
            //console.log("fishmove", time, this.posCur, this.fishid, Date.now(), this.pos[this.posCur-2], this.pos[this.posCur-1], this.mv.x, this.mv.y, this.pos[this.posCur], this.pos[this.posCur+1]);
            let rot = 180/(Math.PI/Math.atan2(this.pos[this.posCur+1]-this.mv.y,this.pos[this.posCur]-this.mv.x)) + 90;
            if(rot > 180) rot = rot - 360;
            if(rot < -180) rot = rot + 360;
            egret.Tween.get(this.mv).to({rotation:rot}, 400).to({x:this.pos[this.posCur], y:this.pos[this.posCur+1]}, time).call(this.finish, this);
        }

        /** 是否完成 */
        public isClose():boolean{
            return this.posCur >= this.posCount;
        }

        /** 鱼死亡 */
        public dead():void{
            let lb = this.mv["getFrameLabelByFrame"]("Dead");
            if(lb) this.mv.gotoAndPlay("Dead", 1);
            else this.mv.gotoAndPlay("Move", 1);
            this.posCur = this.posCount;
            this.mv.addEventListener(egret.Event.COMPLETE, this.finish, this);
        }
    }
}